package com.sherlocky.util;

import org.apache.commons.lang.StringUtils;


import com.rework.joss.persistence.convention.ConventionUtils;
import com.rework.joss.persistence.convention.ExpressionUtil;

public class PaginationUtil {
	
	/**
	 * 构造分页的html，格式：上一页 1... 3 4 5 6 7...总数 下一页
	 * @param count 记录总数
	 * @param perpage 每页显示记录数
	 * @param curpage 当前页码
	 * @param pageItemCount 显示的页码数量，例如上面就是5
	 * @param href 页码的链接
	 * @return
	 */
	public static String multi(int count, int perpage, int curpage, int pageItemCount, String href) {
		StringBuffer multipage = new StringBuffer();
		//共有多少页
		int pages = 1;
		if(count > perpage) {
			int realpages = (count % perpage == 0) ? (count / perpage) : (count / perpage + 1);
			pages = realpages;
			//页码显示的起始页码和截止页码
			int from = 1;
			int to = pages;
			//当前页码距离起始页码的偏移量
			int offset = 2;
			if(pageItemCount <= pages) {
				from = curpage - offset;
				to = from + pageItemCount - 1;
				if(from < 1) {
					to = curpage + 1 - from;
					from = 1;
					if(to - from < pageItemCount) {
						to = pageItemCount;
					}
				}else if(to > pages) {
					//不能超过页码数量
					from = pages - pageItemCount + 1;
					to = pages;
				}
			}
			if(curpage > 1) {
				//上一页
				multipage.append("<a href=\"" + getPageHref(href, curpage - 1) + "\" class=\"prev\">上一页</a>");
			}
			if(from > 2) {
				multipage.append("<a href=\"" + getPageHref(href, 1) + "\" class=\"first pagenum\">1</a>...");
			}else if(curpage - offset > 1 && from != 1){
				multipage.append("<a href=\"" + getPageHref(href, 1) + "\" class=\"first pagenum\">1</a>");
			}
			//多个页码显示
			for(int i = from; i <= to; i++) {
				if(i == curpage) {
					multipage.append("<span class=\"current\">" + i +"</span>");
				}else if(i != pages){
					multipage.append("<a href=\"" + getPageHref(href, i) +"\" class=\"pagenum\">" + i +"</a>");
				}
			}
			if(pages - to > 1) {
				multipage.append("...<a href=\"" + getPageHref(href, pages) + "\" class=\"last pagenum\">" + realpages +"</a>");
			}
			else if (curpage != pages) {
				//这里显示实际页码数
				multipage.append("<a href=\"" + getPageHref(href, pages) + "\" class=\"last pagenum\">" + realpages +"</a>");
			}
			if(curpage < pages) {
				//下一页
				multipage.append("<a href=\"" + getPageHref(href, curpage + 1) + "\" class=\"next\">下一页</a>");
			}
			/*if(multipage.length() > 0) {
				multipage.insert(0, "<em>&nbsp;" + count + "&nbsp;</em>");
			}*/
		}
		return multipage.toString();
	}
	
	public static String getPageHref(String href, int page) {
		if(href.indexOf("javascript:") == 0) {
			if(href.indexOf("(") > 0) {
				return ExpressionUtil.parse(href, ConventionUtils.toMap("page", page));
			}else{
				return href + "(" + page + ")";
			}
		}
		return href + (href.indexOf("?") >= 0? "&" : "?") + "page=" + page;
	}
	
	/**
	 * 构造分页的html，格式：总数 << 1 2 3 4 5 >> ...最后页码
	 * @param count 记录总数
	 * @param perpage 每页显示记录数
	 * @param curpage 当前页码
	 * @param href 页码的链接
	 * @return
	 */
	public static String multi(int count, int perpage, int curpage, String href) {
		return multi(count, perpage, curpage, 5, href);
	}
	
	//处理分页
	public static final String smulti(int start, int perpage, int count, String url, String ajaxdiv) {
		ajaxdiv = StringUtils.defaultIfEmpty(ajaxdiv, "");
		
		int last = -1;
		int next = -1;
		int begin = -1;
		int end = -1;
		StringBuffer html = new StringBuffer("");
		if(start > 0) {
			if(count <= 0) {
				
			} else {
				last = start - perpage;
			}
		}

		boolean showhtml = false;
		if(count == perpage) {
			next = start + perpage;
		}
		begin = start + 1;
		end = start + count;

		if(begin >= 0) {
			if(last >= 0) {
				showhtml = true;
				html.append("<a href=\""+url+"\">|&lt;</a> <a href=\""+url+"&start="+last+"\">&lt;</a> ");
			} else {
				html.append("&lt;");
			}
			html.append(" "+begin+"~"+end+" ");
			if(next >= 0) {
				showhtml = true;
				html.append(" <a href=\""+url+"&start="+next+"\">&gt;</a>");
			} else {
				html.append(" &gt;");
			}
		}

		return showhtml? html.toString() : "";
	}
}
